<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系统权限</title>
    <script src="../../../resources/app/all.js" type="text/javascript"></script>
    <style type="text/css">
        a:link {
            text-decoration: none;
        }

        a:visited {
            text-decoration: none;
        }

        a:hover {
            text-decoration: none;
        }

        a:active {
            text-decoration: none;
        }
    </style>
</head>
<body>
<table id="auth-table" class="layui-table" lay-filter="auth-table"></table>

<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addTop"><i class="layui-icon layui-icon-add-1"></i>新增目录
        </button>
    </div>
</script>
<script type="text/html" id="auth-state">
    {{# if(d.type!=3){ }}
    <a class="layui-btn  layui-btn-xs" lay-event="subset"><i class="layui-icon layui-icon-add-1"></i>新增子级</a>
    {{#  } }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>

<script>
    layui.config({
        base: pathName + '/resources/module/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    }).use(['table', 'treetable'], function () {
        var table = layui.table,
            treetable = layui.treetable;
        var renderTable = function () {
            layer.load();
            ToolUtil.POST({
                url: global.api_url + '/sys/sysPermission/page',
                type: 'POST',
                data: JSON.stringify({}),
                contentType: 'application/json;charset=UTF-8',
                async: false,
                success: function (obj) {
                    if (obj && obj.code === '00') {
                        initData(obj.data);
                    } else if (obj.code === '98' || obj.code === '97') {
                        location.href = global.loginUrl;
                    } else {
                        layer.alert(obj.msg,{icon:5});
                    }

                }
            });
            layer.closeAll()
        };

        function initData(data) {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'parentId',
                treeDefaultClose: true,
                elem: '#auth-table',
                page: false,
                url: '',
                data: data,
                toolbar: '#toolbar',
                height: 'full-20',
                defaultToolbar: [],
                cols: [
                    [{type: 'numbers'},
                        {field: 'name', minWidth: 200, title: '名称'},
                        {
                            field: 'icon', width: 120, align: 'center', templet: function (d) {
                                return d.icon == null ? ''
                                    : '<i class="fa  ' + d.icon
                                    + '"></i>';
                            }, title: '图标'
                        },
                        {
                            field: 'type', width: 120, align: 'center', templet: function (d) {
                                if (d.type == 1) {
                                    return '目录';
                                } else if (d.type == 2) {
                                    return '菜单';
                                } else if (d.type == 3) {
                                    return '按钮';
                                }
                            }, title: '类型'
                        },
                        {field: 'url', minWidth: 120, title: '地址'},
                        {field: 'permission', width: 200, title: '权限标识'},
                        {templet: '#auth-state', align: 'left', title: '操作'}
                    ]
                ],
                done: function (res, curr, count) {
                    layer.closeAll('loading');
                }
            });
        }

        renderTable();
        table.on('toolbar(auth-table)', function (obj) {
            if (obj.event === 'addTop') {
                layer.open({
                    type: 2,
                    title: '新增目录',
                    maxmin: false,
                    data: {
                        id: 0,
                        act: "add"
                    },
                    shadeClose: false, // 点击遮罩关闭层
                    area: ['60%', '80%'],
                    content: 'edit.html'
                });
            }
        });

        table.on('tool(auth-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'subset') {
                layer.open({
                    type: 2,
                    title: '新增子集',
                    maxmin: false,
                    data: {
                        id: data.id,
                        act: "add"
                    },
                    shadeClose: false, // 点击遮罩关闭层
                    area: ['60%', '80%'],
                    content: 'edit.html'
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: '修改权限信息',
                    maxmin: false,
                    data: {
                        id: data.id,
                        act: "edit",
                        data: data
                    },
                    shadeClose: false, // 点击遮罩关闭层
                    area: ['60%', '80%'],
                    content: 'edit.html'
                });
            } else if (obj.event === 'del') {
                layer.confirm('确定要删除选中的记录？', {
                    title: '提示',
                    btn: ['确定', '取消']
                }, function () {
                    ToolUtil.DELETE({
                        url: global.api_url + '/sys/sysPermission/' + data.id,
                        async: false,
                        success: function (obj) {
                            if (obj && obj.code === '00') {
                                location.reload();
                            } else if (obj.code === '98' || obj.code === '97') {
                                location.href = global.loginUrl;
                            } else {
                                layer.alert(obj.msg,{icon:5});
                            }
                        },
                        complete:function () {
                            layer.closeAll();
                        }
                    });

                });
            }
        });

    });


</script>

</body>
</html>